package com.atguigu.test01;

class test{
    private int num = 0;

    private char aChar = 'A'-1;

    private int flag = 1;

    public synchronized void addNum1() throws InterruptedException {
        Thread.sleep(500);
        while (flag!=1){
            wait();
        }
        num++;
        System.out.println(Thread.currentThread().getName()+" "+num);
        flag = 2;
        notifyAll();
    }

    public synchronized void addNum2() throws InterruptedException {
        Thread.sleep(500);
        while (flag!=2){
            wait();
        }
        num++;
        System.out.println(Thread.currentThread().getName()+" "+num);
        flag = 3;
        notifyAll();
    }

    public synchronized void addChar() throws InterruptedException {
        Thread.sleep(500);
        while (flag!=3){
            wait();
        }
        aChar++;
        System.out.println(Thread.currentThread().getName()+" "+aChar);
        flag=1;
        notifyAll();
    }
}
public class test02 {

    public static void main(String[] args) {
        test test = new test();
        new Thread(() -> {
            for (int i = 0; i < 26; i++) {
                try {
                    test.addNum1();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        },"num1").start();

        new Thread(() -> {
            for (int i = 0; i < 26; i++) {
                try {
                    test.addNum2();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        },"num2").start();

        new Thread(() -> {
            for (int i = 0; i < 26; i++) {
                try {
                    test.addChar();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        },"chars").start();
    }
}
